home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / ch_hashi.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  4.0 KB  |  169 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  ch_hashing.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16.  
  17.  
  18. //------------------------------------------------------------------------------
  19. // Hashing with Chaining
  20. // 
  21. // Walter Zimmer   (1989)
  22. //------------------------------------------------------------------------------
  23.  
  24. #ifndef CHHASHINGH
  25. #define CHHASHINGH
  26.  
  27. #include <LEDA/basic.h>
  28. #include <LEDA/list.h>
  29.  
  30. #define NOTEST
  31.  
  32. #ifdef TEST
  33. #define TRACE(x)  cout << form x
  34. #define TRACE2(x,ebene)  if ( debug >= (ebene) ) cout << form  x
  35. #define TRACE_FUNC(x) x
  36. // extern int debug ; muss im testprogramm definiert werden , falls
  37. // Trace2(x,ebene) benutzt wird
  38. #else 
  39. #define TRACE(x) 
  40. #define TRACE2(x,ebene) 
  41. #define TRACE_FUNC(x) 
  42. #endif
  43.  
  44.  
  45. //--------------------------------------------------------------------
  46. // some declarations and type definitions
  47. //--------------------------------------------------------------------
  48.  
  49.  
  50. class ch_hashing_el {
  51.   ent k;
  52.   ent e;
  53.   friend class ch_hashing;
  54.  
  55.   public:
  56.  
  57.   ch_hashing_el(ch_hashing_el* p)     
  58.   { 
  59.     k = p->k; 
  60.     e = p->e; 
  61.    }
  62.  
  63.   ch_hashing_el(ent K = 0, ent E = 0) 
  64.   { 
  65.     k = K; 
  66.     e = E; 
  67.    }
  68.  
  69.   OPERATOR_NEW(2)
  70.   OPERATOR_DEL(2)
  71.  
  72. };
  73.  
  74. typedef ch_hashing_el* list2_el;
  75. declare(list,list2_el);
  76. typedef list(list2_el) hash_list;
  77. typedef hash_list* hash_list_ptr;
  78.  
  79. typedef int (*hash_fct) (ent&);
  80.  
  81. extern int default_hash_fct(ent&);
  82.  
  83. const int default_hash_tablesize = 1000;
  84.  
  85. //--------------------------------------------------------------------
  86. // class ch_hashing
  87. //--------------------------------------------------------------------
  88.  
  89. class ch_hashing {
  90.   
  91.    hash_list_ptr* hash_table;
  92.    int table_size;           
  93.    int divisor;   // divisor = 2^n-1 = 00..0011.11  
  94.                   // => statt " modulo table_size " einfach " & divisor"
  95.    hash_fct f;
  96.    hash_list_ptr* iterator;
  97.    int counter;
  98.  
  99.    int round(int) const;
  100.    hash_list_ptr* get_list(ent) const;
  101.    list_item      get_list_el(ent,hash_list*) const;
  102.    void new_size_or_fct(int, hash_fct);
  103.  
  104.    virtual int cmp(ent& x, ent& y) const { return int(x) - int(y); }
  105.    virtual void clear_key(ent& x)  const { x=0; }
  106.    virtual void clear_inf(ent& x)  const { x=0; }
  107.    virtual void copy_key(ent& x)   const { x=x; }
  108.    virtual void copy_inf(ent& x)   const { x=x; }
  109.    virtual void print_key(ent& x)  const { cout << int(x); }
  110.  
  111.    public:
  112.  
  113.    ch_hashing_el* lookup(ent) const;
  114.    ch_hashing_el* insert(ent,ent);
  115.    ch_hashing_el* del(ent);
  116.  
  117.    bool member(ent x)   const  { return ( lookup(x) ? true : false ); } 
  118.  
  119.    ent& access(ent);
  120.  
  121.    ent  key(ch_hashing_el* p)  const { return p->k; }
  122.    ent  inf(ch_hashing_el* p)  const { return p->e; }
  123.    ent& info(ch_hashing_el* p)       { return p->e; }
  124.  
  125.    bool change_obj(ent x, ent y);
  126.    void change_inf( ch_hashing_el* p , ent y) { copy_inf(p->e = y) ; }
  127.  
  128.    bool empty()  const { return counter ? false : true ; } 
  129.    int   size()  const { return counter; } 
  130.  
  131.    int  tablesize() const { return table_size ; }
  132.  
  133.    void print() const ;
  134.  
  135.    float average_list_length()  const { return float(counter) / table_size ; }
  136.  
  137.    void init_iterator();
  138.  
  139.    ch_hashing_el* move_iterator();
  140.  
  141.    void new_table_size(int Tablesize) { new_size_or_fct(Tablesize,f); }
  142.  
  143.    void new_hash_fct(hash_fct F)      { new_size_or_fct(table_size,F); }
  144.  
  145.    void new_size_and_fct(int Tablesize ,hash_fct F)
  146.                                       { new_size_or_fct(Tablesize,F); }
  147.  
  148.    void clear();
  149.    void remove();
  150.  
  151.    ch_hashing();
  152.    ch_hashing(int);
  153.    ch_hashing(hash_fct);
  154.    ch_hashing(int,hash_fct);
  155.  
  156.    ~ch_hashing() { remove(); }    //  neben clear auch remove implem.
  157.  
  158. };  // end of class ch_hashing
  159.  
  160.  
  161.  
  162. #define forall_in_ch_hashing(k,t)\
  163. for( (t).init_iterator() ; (k)=(t).move_iterator() ; )
  164.  
  165.  
  166.  
  167. #endif CHHASHINGH
  168.